package com.ligongmusic.util;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class ActivityManager {
	private static long startTime = 0;
	private static long endTime = 0;
	public static List<Activity> activitys = new ArrayList<Activity>();
	public static void startActivity(Activity mContext, Class<? extends Activity> clazz){
		// 如果跳转的界面是应用主界面，则释放所有开启过的activity
//		if(clazz == MapActivity.class || clazz == PersonalCenterActivity.class || clazz == GoodsListActivity.class || clazz == OrderSelectActivity.class){
//			while(activitys.size() > 0 && activitys.get(0) != null)
//        		activitys.remove(0).finish();
//		}
		activitys.add(mContext);
		Intent intent = new Intent(mContext, clazz);
		mContext.startActivity(intent);
	}
	
	public static void startActivity(Activity mContext, Class<? extends Activity> clazz, Bundle bundle){
		// 如果跳转的界面是应用主界面，则释放所有开启过的activity
//		if(clazz == MapActivity.class || clazz == PersonalCenterActivity.class || clazz == GoodsListActivity.class || clazz == OrderSelectActivity.class){
//			while(activitys.size() > 0 && activitys.get(0) != null)
//        		activitys.remove(0).finish();
//		}
		activitys.add(mContext);
		Intent intent = new Intent(mContext, clazz);
		intent.putExtras(bundle);
		mContext.startActivity(intent);
	}
	
	
	public static void startActivityForResult(Activity mContext, Class<? extends Activity> clazz, int requestCode){
		activitys.add(mContext);
		Intent intent = new Intent(mContext, clazz);
		mContext.startActivityForResult(intent, requestCode);
	}
	
	public static void startActivityForResult(Activity mContext, Class<? extends Activity> clazz, Bundle bundle, int requestCode){
		activitys.add(mContext);
		Intent intent = new Intent(mContext, clazz);
		intent.putExtras(bundle);
		mContext.startActivityForResult(intent, requestCode);
	}
	
	/**
	 * 全局返回以及退出应用处理
	 */
	public static void actionBack(Activity activity){
		Class<? extends Activity> clazz = activity.getClass();
		if (/*clazz == MapActivity.class || clazz == PersonalCenterActivity.class || clazz == GoodsListActivity.class || clazz == OrderSelectActivity.class*/true) {
            if (startTime == 0) {
            	 startTime = System.currentTimeMillis();
                 endTime = 0;
                 Toast.makeText( activity, "再按一次退出应用", Toast.LENGTH_SHORT).show();
            } else {
                endTime = System.currentTimeMillis();
                if (endTime - startTime < 1000) {
                	while(activitys.size() > 0 && activitys.get(0) != null){
                		Activity remove = activitys.remove(0);
                		remove.finish();
                	}
                	activity.finish();
                	return;
                } else {
                	 startTime = System.currentTimeMillis();
                     endTime = 0;
                     Toast.makeText( activity, "再按一次退出应用", Toast.LENGTH_SHORT).show();
                }
            }
		} else {
			if(activitys.contains(activity))
				activitys.remove(activity);
			activity.finish();
		}
	}
}